#include "documentwidget.h"
#include <QDebug>

DocumentWidget::DocumentWidget(PDFKit *p, QWidget *parnt) : pdf(p), QWidget(parnt)
{
    setStyleSheet("background-color:white");
    QDomDocument *doc = pdf->document->toc();
    if(doc == NULL)
    {
        qDebug() << "get toc fail";
        return ;
    }
/*
    QDomNode domNode = domdoc->firstChild();
    rootItem = new TocTreeItem("domNode.toElement().tagName()");
    TocTreeItem* treeItem;
    for(QDomNode childNode = domNode; !childNode.isNull(); childNode = childNode.nextSibling())
    {
        QString strDom = childNode.toElement().tagName();
        treeItem = new TocTreeItem(strDom, rootItem);
        rootItem->appendChild(treeItem);
        parserDom(&domNode, treeItem);
    }
*/
    QTreeView *treeView = new QTreeView();
    model = new QStandardItemModel();//创建模型指定父类
    treeView->setModel(model);



    int i=0;
//    callback(i, doc->firstChild());


    QDomNode domNode = doc->firstChild();
    for(QDomNode childNode=domNode; !childNode.isNull(); childNode=childNode.nextSibling())
    {
        QString str = childNode.toElement().tagName();
        model->setItem(i, 0, new QStandardItem(str));

        callback(i, childNode);
/*
        j = 0;
        for(QDomNode grandChild=domNode.firstChild(); !grandChild.isNull(); grandChild=grandChild.nextSibling())
        {
            QString str = grandChild.toElement().tagName();
            model->item(i, 0)->setChild(j, 0, new QStandardItem(str));
//            model->setItem(i, j, new QStandardItem(str));
            j++;
        }
*/
        i++;
    }


/*
    model->setItem(0,0,new QStandardItem("one"));
    model->item(0,0)->setChild(0,0,new QStandardItem("二级1"));
    model->item(0,0)->setChild(1,0,new QStandardItem("二级2"));
    model->setItem(1,0,new QStandardItem("two"));
    model->item(1,0)->setChild(0,0,new QStandardItem("二级1"));
*/



    QVBoxLayout *mainLayout = new QVBoxLayout();
    setLayout(mainLayout);
    mainLayout->addWidget(treeView);
}


void DocumentWidget::callback(int &i, QDomNode doc)
{
    int j = 0;
    for(QDomNode grandChild=doc.firstChild(); !grandChild.isNull(); grandChild=grandChild.nextSibling())
    {
        QString str = grandChild.toElement().tagName();
        model->item(i, 0)->setChild(j, 0, new QStandardItem(str));
        callback(i, grandChild);

//        qDebug() << str;
        j++;
    }
}


